package lion.dev.image.format;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;

import com.sun.jimi.core.Jimi;
import com.sun.jimi.core.JimiException;
import com.sun.jimi.core.JimiWriter;
import com.sun.jimi.core.options.JPGOptions;

import lion.dev.image.FileFormatWriter;

public class FileFormatJPEG extends FileFormatWriter {

	private int quality = 95; // 设置为100后会使文件体积增大许多

	public FileFormatJPEG() {
		super("JPEG", new String[] { "image/jpeg" }, new String[] { ".jpg", ".jpeg" });
	}

	public boolean canRead(byte[] fileStart) {

		return false;
	}

	@Override
	public void writeImage(OutputStream out, BufferedImage image) throws IOException {

		try {
			JPGOptions options = new JPGOptions();
			options.setQuality(this.quality);
			JimiWriter writer = Jimi.createTypedJimiWriter("image/jpeg");
			writer.setSource(image);
			writer.setOptions(options);
			writer.putImage(out);
		} catch (JimiException e) {
			throw new IOException(e + ": " + e.getMessage());
		}
	}

}
